/**
 *
 *  Copyright (c) 2011, AstroDAbis
 *  All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without modification,
 *  are permitted provided that the following conditions are met:
 *
 *      * Redistributions of source code must retain the above copyright notice,
 *        this list of conditions and the following disclaimer.
 *      * Redistributions in binary form must reproduce the above copyright notice,
 *        this list of conditions and the following disclaimer in the documentation
 *        and/or other materials provided with the distribution.
 *
 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 *  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 *  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 *  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 *  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 *  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 *  OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 *  OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
package org.jiscinvolve.astrodabis.galium.ogsadai.activity.client;

import uk.org.ogsadai.data.StringData;
import uk.org.ogsadai.activity.ActivityName;
import uk.org.ogsadai.resource.ResourceID;

import uk.org.ogsadai.client.toolkit.Activity;
import uk.org.ogsadai.client.toolkit.ActivityOutput;
import uk.org.ogsadai.client.toolkit.SingleActivityOutput;
import uk.org.ogsadai.client.toolkit.activity.ActivityInput;
import uk.org.ogsadai.client.toolkit.activity.BaseActivity;
import uk.org.ogsadai.client.toolkit.activity.SimpleActivityInput;
import uk.org.ogsadai.client.toolkit.activity.SimpleActivityOutput;
import uk.org.ogsadai.client.toolkit.exception.ActivityIOIllegalStateException;
import uk.org.ogsadai.client.toolkit.exception.DataSourceUsageException;
import uk.org.ogsadai.client.toolkit.exception.DataStreamErrorException;
import uk.org.ogsadai.client.toolkit.exception.UnexpectedDataValueException;

/**
 * Client toolkit proxy for:
 * <code>
 * org.jiscinvolve.astrodabis.galium.ogsadai.activity.server.CreateTapResourceActivity
 * </code>
 *
 * Based on the example
 * <a href='http://ogsa-dai.sourceforge.net/documentation/ogsadai4.0/ogsadai4.0-gt/DeveloperWriteResourceFactory.html'>resource factory activity</a> 
 * from the OGSA-DAI documentation.
 *
 */
public class CreateTapResource
    extends BaseActivity implements Activity
    {

    /**
     * Default activity name.
     *
     */
    public final static ActivityName DEFAULT_ACTIVITY_NAME = 
        new ActivityName(
            "astrodabis.CreateTapResource"
            );

    /**
     * Activity input - the resource identifier.
     *
     */
    private ActivityInput astroResourceId;

    /**
     * Activity input - the template identifier.
     *
     */
    private ActivityInput astroTemplateId;

    /**
     * Activity input - the TAP service endpoint.
     *
     */
    private ActivityInput astroTapEndpoint;

    /**
     * Resource name output.
     *
     */
    private final ActivityOutput astroResultOutput;

    /**
     * Public constructor.
     *
     */
    public CreateTapResource()
        {
        super(DEFAULT_ACTIVITY_NAME);
        astroResourceId = new SimpleActivityInput(
            "resourceId",
            SimpleActivityInput.OPTIONAL
            );
        astroTemplateId = new SimpleActivityInput(
            "templateId",
            SimpleActivityInput.OPTIONAL
            );
        astroTapEndpoint = new SimpleActivityInput(
            "endpoint"
            );
        astroResultOutput = new SimpleActivityOutput(
            "result"
            );
        }

    /**
     * Adds a resource ID input.
     *
     * @param resourceID
     *     ID of the new resource.
     */
    public void addResourceId(final ResourceID ident)
        {
        astroResourceId.add(
            new StringData(
                ident.toString()
                )
            );
        }
    
    /**
     * Connects the resource ID input to the given output.
     * 
     * @param output 
     *     Output to connect the input to.
     */
    public void connectResourceIdInput(final SingleActivityOutput output)
        {
        astroResourceId.connect(
            output
            );
        }
    
    /**
     * Adds a template ID input.
     * 
     * @param templateID
     *     Resource template ID.
     */
    public void addTemplateId(final ResourceID ident)
        {
        astroTemplateId.add(
            new StringData(
                ident.toString()
                )
            );
        }
    
    /**
     * Connects the template ID input to the given output.
     * 
     * @param output 
     *     Output to connect the input to.
     */
    public void connectTemplateIdInput(final SingleActivityOutput output)
        {
        astroTemplateId.connect(output);        
        }

    /**
     * Adds a service endpoint input.
     * 
     * @param endpoint
     *     The TAP service endpoint.
     */
    public void addTapEndpoint(final String endpoint)
        {
        astroTapEndpoint.add(
            new StringData(
                endpoint
                )
            );
        }
    
    /**
     * Connects the service endpoint input to the given output.
     * 
     * @param output 
     *     Output to connect the input to.
     */
    public void connectTapEndpointInput(final SingleActivityOutput output)
        {
        astroTapEndpoint.connect(output);        
        }

    /**
     * Gets the activity's result output.  This method is use to
     * connect the output to other activities.
     * 
     * @return the activity's output.
     */
    public SingleActivityOutput getResultOutput()
        {
        return astroResultOutput.getSingleActivityOutputs()[0];
        }

    /**
     * Checks whether or not there are any unread output values.
     * 
     * @return <code>true</code> if there are unread output value, 
     *         <code>false</code> if there are none.
     * @throws DataStreamErrorException 
     *     If there is an error on the data stream.
     * @throws UnexpectedDataValueException
     *     If there is an unexpected data value on the data stream.
     * @throws DataSourceUsageException
     *     If there is an error reading from a data source.
     */
    public boolean hasNextResult() 
        throws DataStreamErrorException, 
               UnexpectedDataValueException, 
               DataSourceUsageException
        {
        return astroResultOutput.getDataValueIterator().hasNext();
        }
    
    /**
     * Gets the next resource ID from the output.
     * 
     * @return resource ID.
     * @throws DataStreamErrorException 
     *     If there is an error on the data stream.
     * @throws UnexpectedDataValueException
     *     If there is an unexpected data value on the data stream.
     * @throws DataSourceUsageException
     *     If there is an error reading from a data source.
     */
    public ResourceID nextResult() 
        throws DataStreamErrorException, 
               UnexpectedDataValueException, 
               DataSourceUsageException
        {
        String resourceID = astroResultOutput.getDataValueIterator().nextAsString();
        return new ResourceID(resourceID);
        }
    
    /**
     * {@inheritDoc}
     *
     * No-op.
     */ 
    protected void validateIOState()
    throws ActivityIOIllegalStateException
        {
        // Empty method.
        }

    /**
     * {@inheritDoc}
     *
     */ 
    protected ActivityInput[] getInputs()
        {
        return new ActivityInput[]
            {
            astroResourceId,
            astroTemplateId,
            astroTapEndpoint
            };
        }

    /**
     * {@inheritDoc}
     *
     */ 
    protected ActivityOutput[] getOutputs()
        {
        return new ActivityOutput[]
            {
            astroResultOutput
            };
        }
    }
